package com.enjoyor.soa.traffic.server.efoms.dubbo.manage;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;
import com.enjoyor.soa.traffic.api.dto.efoms.WorkordersRecordInfoDto;
import com.enjoyor.soa.traffic.core.efoms.domain.WorkordersInfo;
import com.enjoyor.soa.traffic.core.efoms.domain.WorkordersRecordInfo;
import com.enjoyor.soa.traffic.core.efoms.enums.EnumDicPublic;
import com.enjoyor.soa.traffic.core.efoms.service.IWorkordersInfoService;
import com.enjoyor.soa.traffic.core.efoms.service.IWorkordersRecordService;
import com.enjoyor.soa.traffic.process.vo.ProcessVo;
import com.enjoyor.soa.traffic.server.efoms.dubbo.invoke.DubboProcessServiceInvoke;
import com.enjoyor.soa.traffic.server.efoms.dubbo.listener.MessagePushClient;
import com.enjoyor.soa.traffic.server.efoms.helper.ObjectSwitchHelper;
import com.enjoyor.soa.traffic.util.enums.EnumAppCode;
import com.enjoyor.soa.traffic.util.helper.LoggerHelper;
import com.enjoyor.soa.traffic.util.helper.ResultHelper;
import com.enjoyor.soa.traffic.util.pojo.ResultPojo;

@Service
public class ManageWorkordersRecordService {

	
    @Autowired
    private IWorkordersInfoService workordersInfoService;
    
    @Autowired
    private IWorkordersRecordService workordersRecordService;
    
    @Autowired 
    private ManageDevStatusInfoService manageDevStatusInfoService;
	
	@Autowired
	private DubboProcessServiceInvoke dubboProcessServiceInvoke;

	@Autowired
	private MessagePushClient messagePushClient;
	
	public ResultPojo insertWorkordersRecord(WorkordersRecordInfoDto workordersRecordDto, String isRecover, 
			String roleName, String personName){
		try {
			String result = "";
	    	WorkordersRecordInfo workordersRecord = ObjectSwitchHelper.workordersRecordDtoToPojo(workordersRecordDto);
	    	String workordersId = workordersRecord.getWorkordersId();
	    	String operTypeCode = workordersRecord.getOperTypeCode();
	    	String deadlineDate = workordersRecord.getDeadlineDate();
	    	if(EnumDicPublic.ORD_OPER_DELETE.getKey().equals(operTypeCode)) {
	    		//撤销 
	    		ProcessVo processVo = dubboProcessServiceInvoke.submitProcess(workordersId, null, operTypeCode);
	        	if(processVo.getResult()) {
	        		//String workordersStatusCode = processVo.getTaskDefinitionKey();
	        		String workordersStatusCode = EnumDicPublic.ORD_ISDELETE.getKey();
					result = workordersRecordService.insertWorkordersRecord(workordersRecord, workordersStatusCode, 
							null, personName);
	        	}else {
	        		return ResultHelper.getResult(EnumAppCode.WORK_FLOW_ERROR, "流程引擎执行错误！");
	        	}
	        	
	        	//修改设备状态数据--使用中、正常
				WorkordersInfo workordersInfo = workordersInfoService.selectWorkordersBaseById(workordersId);
				if(null != workordersInfo) {
					manageDevStatusInfoService.saveDevStatusInfo(
							workordersInfo.getDevCategoryCode(),workordersInfo.getDevCategoryName(),
							workordersInfo.getDevTypeCode(), workordersInfo.getDevTypeName(),
							workordersInfo.getDevId(), workordersInfo.getDevName(),
							EnumDicPublic.DEV_REP_STATUS_USEING.getKey(), 
							EnumDicPublic.DEV_REP_STATUS_USEING.getValue(),
							EnumDicPublic.DEVICE_STATUS_NAOMAL.getKey(), 
							EnumDicPublic.DEVICE_STATUS_NAOMAL.getValue(), personName);
				}
				//统计催办数据
	    		messagePushClient.ordMsgPress();
	        	//统计进度反馈数据
	    		messagePushClient.ordMsgFackback();
	        	//统计延期申请数据
	    		messagePushClient.ordMsgPostpone();
	    	}else if(EnumDicPublic.ORD_OPER_PRESS.getKey().equals(operTypeCode)) {
	    		//催办
	    		result = workordersRecordService.insertWorkordersRecord(workordersRecord, null,  null, personName);
	    		//统计催办数据
	    		messagePushClient.ordMsgPress();
	    		//工单催办提醒
	    		messagePushClient.getOrdersPressPushMessage();
	    	}else if(EnumDicPublic.ORD_OPER_DISPATCH.getKey().equals(operTypeCode)) {
	    		//派发
	    		ProcessVo processVo = dubboProcessServiceInvoke.submitProcess(workordersId, null, operTypeCode);
	        	if(processVo.getResult()) {
	        		String workordersStatusCode = processVo.getTaskDefinitionKey();
					result = workordersRecordService.insertWorkordersRecord(workordersRecord, workordersStatusCode, 
							deadlineDate, personName);
					//工单派发提醒；
					messagePushClient.getOrdersSellPushMessage();
	        	}else {
	        		return ResultHelper.getResult(EnumAppCode.WORK_FLOW_ERROR, "流程引擎执行错误！");
	        	}
	    	}else if(EnumDicPublic.ORD_OPER_POSTPONE.getKey().equals(operTypeCode)) {
	    		//延期申请
	    		result = workordersRecordService.insertWorkordersRecord(workordersRecord, null, null, personName);
	    		//延期申请提醒；
	    		messagePushClient.getOrdersDelayPushMessage();
	    		//统计延期数据
	    		messagePushClient.ordMsgPostpone();
	    	}else if(EnumDicPublic.ORD_OPER_CHECK.getKey().equals(operTypeCode)) {
	    		//延期审核
	    		result = workordersRecordService.insertWorkordersRecord(workordersRecord, null, 
						deadlineDate, personName);
	    		//延期审核提醒；
	    		messagePushClient.getOrdersDelayAuditPushMessage();
	    		//统计延期数据
	    		messagePushClient.ordMsgPostpone();
	    	}else if(EnumDicPublic.ORD_OPER_FACKBACK.getKey().equals(operTypeCode)) {
	    		//反馈
	    		if(EnumDicPublic.OPER_FACKBACK_ING.getKey().equals(workordersRecord.getOperResultCode())) {
	    			result = workordersRecordService.insertWorkordersRecord(workordersRecord, personName);
	    			//统计进度反馈数据
	        		messagePushClient.ordMsgFackback();
	        		//进度反馈提醒；
	        		messagePushClient.getOrdersScheTickPushMessage();
	    		}else if(EnumDicPublic.OPER_FACKBACK_OVER.getKey().equals(workordersRecord.getOperResultCode())) {
	    			ProcessVo processVo = dubboProcessServiceInvoke.submitProcess(workordersId, null, operTypeCode);
	            	if(processVo.getResult()) {
	            		String workordersStatusCode = processVo.getTaskDefinitionKey();
	    				result = workordersRecordService.insertWorkordersRecord(workordersRecord, workordersStatusCode, 
	    						null, personName);
	            	}else {
	            		return ResultHelper.getResult(EnumAppCode.WORK_FLOW_ERROR, "流程引擎执行错误！");
	            	}
	
	            	//统计催办数据
	        		messagePushClient.ordMsgPress();
	            	//统计进度反馈数据
	        		messagePushClient.ordMsgFackback();
	            	//统计待确认数据
	        		messagePushClient.ordMsgSure();
	            	//统计延期申请数据
	        		messagePushClient.ordMsgPostpone();
	    		}
	    	}else if(EnumDicPublic.ORD_OPER_SURE.getKey().equals(operTypeCode)) {
	    		//确认
	    		String operTypeFlow = "";
	    		if(EnumDicPublic.OPER_SUREPASS.getKey().equals(workordersRecord.getOperResultCode())) {
	    			operTypeFlow = EnumDicPublic.ORD_OPER_SUREPASS.getKey();
	    		}else if(EnumDicPublic.OPER_SUREREJECT.getKey().equals(workordersRecord.getOperResultCode())) {
	    			operTypeFlow = EnumDicPublic.ORD_OPER_SUREREJECT.getKey();
	    		}
	    		ProcessVo processVo = dubboProcessServiceInvoke.submitProcess(workordersId, roleName, operTypeFlow);
	        	if(processVo.getResult()) {
	        		String workordersStatusCode = processVo.getTaskDefinitionKey();
	        		if(null == workordersStatusCode) {
	        			//确认通过 --流程结束
	        			workordersStatusCode = EnumDicPublic.ORD_WAITEVALUATE.getKey();
	        			//修改设备状态数据--使用中
	        			String devStatusCode = EnumDicPublic.DEVICE_STATUS_NAOMAL.getKey();
	        			String devStatusName = EnumDicPublic.DEVICE_STATUS_NAOMAL.getValue();
	        			if("0".equals(isRecover)) {
	        				devStatusCode = null;
	        				devStatusName = null;
	        			}
	        			WorkordersInfo workordersInfo = workordersInfoService.selectWorkordersBaseById(workordersId);
	        			if(null != workordersInfo) {
	        				manageDevStatusInfoService.saveDevStatusInfo(
	        						workordersInfo.getDevCategoryCode(),workordersInfo.getDevCategoryName(),
	        						workordersInfo.getDevTypeCode(), workordersInfo.getDevTypeName(),
	        						workordersInfo.getDevId(), workordersInfo.getDevName(),
	    							EnumDicPublic.DEV_REP_STATUS_USEING.getKey(), 
	    							EnumDicPublic.DEV_REP_STATUS_USEING.getValue(),
	    							devStatusCode, devStatusName, personName);
	        			}
	        			
	        		}
					result = workordersRecordService.insertWorkordersRecord(workordersRecord, workordersStatusCode, 
							null, personName);
					//确认提醒；
					messagePushClient.getOrdersFinishedAudit();
					//统计待确认数据
	        		messagePushClient.ordMsgSure();
	        	}else {
	        		return ResultHelper.getResult(EnumAppCode.WORK_FLOW_ERROR, "流程引擎执行错误！");
	        	}
	    		
	    	}else if(EnumDicPublic.ORD_OPER_EVALUATE.getKey().equals(operTypeCode)) {
	    		//评价
	    		String workordersStatusCode = EnumDicPublic.ORD_ISFINISH.getKey();
				result = workordersRecordService.insertWorkordersRecord(workordersRecord, workordersStatusCode, 
						null, personName);
	    	}else {
	    		return ResultHelper.getResult(EnumAppCode.PARAM_ERR, "操作类型参数错误！");
	    	}
	        return ResultHelper.getResult(result);
		} catch (Exception e) {
            LoggerHelper.LOG.error(e);
            return ResultHelper.getResult(e);
        }
	}
	
	public ResultPojo getNextProcess(String workordersId) {
    	try {
			List<ProcessVo> processVoList = dubboProcessServiceInvoke.getNextProcess(workordersId);
	    	String nextOperation = "";
			for(ProcessVo process : processVoList){
				if(!"null".equals(process.getDocumentation()) && process.getDocumentation() != null){
					nextOperation += ',' + process.getDocumentation();
				}
			}
			nextOperation = (nextOperation.length() > 0 ? nextOperation.substring(1) : "");
			return ResultHelper.getResult(nextOperation);
    	} catch (Exception e) {
            LoggerHelper.LOG.error(e);
            return ResultHelper.getResult(e);
        }
    }
}
